/////precompiled////////
#include "precompiled.h"
////////////////////////


#include "prs_FrmGeomSize.h"
#include "prs_FrmDefTemplate.h"
using namespace prs;

// TNodeFrmGeomSize ///////////////////////////////////////////////////////////

// TFrmGeomSizeValue //////////////////////////////////////////////////////////

// TRuleFrmGeomSize ///////////////////////////////////////////////////////////

//
TRuleFrmGeomSize::TType TRuleFrmGeomSize::Type;
TNodeFrmGeomSizeFixed TRuleFrmGeomSize::Fixed; 
TNodeFrmGeomSizeParent TRuleFrmGeomSize::Parent; 
TNodeFrmGeomSizeChildrenSum TRuleFrmGeomSize::ChildrenSum; 
TNodeFrmGeomSizeChildrenMax TRuleFrmGeomSize::ChildrenMax; 
TNodeFrmGeomSizeChildrenMaxBase TRuleFrmGeomSize::ChildrenMaxBase; 
TNodeFrmGeomSizeSiblingsRem TRuleFrmGeomSize::SiblingsRem; 
TNodeFrmGeomSizeSiblingsMax TRuleFrmGeomSize::SiblingsMax; 

//
TRuleFrmGeomSize::TRuleFrmGeomSize(
  snt::text::TContext &SntContext
) 
{
  attr = NULL;
  vEnSetParamAsIdent = true;
  
  snt::TReturn Return;
  snt::TFVoid F;
  snt::text::TNumber N(SntContext);
  snt::text::TIdent I(SntContext);
  snt::text::TSequence T(SntContext);
  snt::text::TEnumSet EnumSet(SntContext);

  RWidth
  | F.New(this,&TRuleFrmGeomSize::FEnableSetParamAsIdent)
    & T.New("width") & T.New("=") 
    & RValue.New() 
    & Return.New()
  ;  
  RHeight
  | F.New(this,&TRuleFrmGeomSize::FEnableSetParamAsIdent)
    & T.New("height") & T.New("=")
    & RValue.New() 
    & Return.New()
  ;  
    
  RValue.Local(vEnSetParamAsIdent)
  | N.NewClosure(this,&TRuleFrmGeomSize::FSetNumber)
    & Return.New()
    
  | EnumSet.New(this,&TRuleFrmGeomSize::FGetEnumSize)
    .SetDoEntry(this,&TRuleFrmGeomSize::FSetEnumSize)
    & Return.New()

  | I.NewClosure(this,&TRuleFrmGeomSize::FSetParamAsIdent).SetEnable(vEnSetParamAsIdent)
    & Return.New()
  ;
}

//
void TRuleFrmGeomSize::FEnableSetParamAsIdent(snt::TParams &Params)
{
  EnableSetParamAsIdent(true);
}

//
bool TRuleFrmGeomSize::FSetNumber(snt::text::TNumber::TFuncClosureDoEntryParams &Params)
{
  Fixed.Size = Params.Number.GetValue().Integer();
  attr->SetValueAsPointer( TNode::newCopyFrom(&Fixed) );
  return true;
}

//
void TRuleFrmGeomSize::FGetEnumSize(snt::text::TEnumSet::TFuncGetEnumSetParams &Params)
{
  Params.OutEnumSet = &Type;
}

//
bool TRuleFrmGeomSize::FSetEnumSize(snt::text::TEnumSet::TFuncDoEntryParams &Params)
{
  TNodeFrmGeomSize::EType EType = (TNodeFrmGeomSize::EType)Type.Get(Params.Token.GetNValue()).I;
  TNodeFrmGeomSize *geomSize;

  switch (EType) {
    case TNodeFrmGeomSize::EType_Parent:
      geomSize = &Parent;
    break;

    case TNodeFrmGeomSize::EType_ChildrenSum:
      geomSize = &ChildrenSum;
    break;

    case TNodeFrmGeomSize::EType_ChildrenMax:
      geomSize = &ChildrenMax;
    break;
    
    case TNodeFrmGeomSize::EType_ChildrenMaxBase:
      geomSize = &ChildrenMaxBase;
    break;
    
    case TNodeFrmGeomSize::EType_SiblingsRem:
      geomSize = &SiblingsRem;
    break;
    
    case TNodeFrmGeomSize::EType_SiblingsMax:
      geomSize = &SiblingsMax;
    break;
    
    default: Except("FSetEnumSize[default]");
  }

  attr->SetValueAsPointer( TNode::newCopyFrom(geomSize) );
  return true;
}

//
bool TRuleFrmGeomSize::FSetParamAsIdent(snt::text::TIdent::TFuncClosureDoEntryParams &Params)
{
  svl::TString Id = Params.Ident.GetValue().String();
  return TNodeDefTemplate::AddLinkAttrByParamId(attr,TFrmGeomSizeValue(),Id,Params.OutError);
}
